package com.tsinghuabigdata.edu.ddmath.module.mylearn.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tsinghuabigdata.edu.commons.cache.CacheManager;
import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.constant.AppConst;
import com.tsinghuabigdata.edu.ddmath.module.mylearn.bean.ImageCorrectBean;
import com.tsinghuabigdata.edu.utils.MD5Util;

import java.util.ArrayList;

/**
 * 把学生图片和批改结果合成一张图片上面
 */
public class CorrectImageUtil {

    private static Bitmap rightBitmap;
    private static Bitmap wrongBitmap;
    private static Bitmap halfBitmap;

    public CorrectImageUtil(Context context){
        if( rightBitmap == null ){
            Resources res = context.getResources();
            rightBitmap = BitmapFactory.decodeResource(res, R.drawable.ico_right_big);
            wrongBitmap = BitmapFactory.decodeResource(res, R.drawable.ico_wrong_big);
            halfBitmap = BitmapFactory.decodeResource(res, R.drawable.ico_half_right);
        }
    }

    /**
     *
     * @param url           原始url
     * @param srcBitmap     原始图片
     * @param correctResult 批改结果
     * @return 合成的图片
     */
    public Bitmap getCorrectBitmap( String url, Bitmap srcBitmap, String correctResult ){
        String md5 = MD5Util.getMD5String( url+correctResult );
        Bitmap bitmap = (Bitmap) CacheManager.getAppCache().getCache( md5 );
        if( bitmap != null ) return bitmap;

        bitmap = createCorrectBitmap( srcBitmap, correctResult );
        if(bitmap!=null) CacheManager.getAppCache().putCache( md5, bitmap, AppConst.MAX_EXPIRE_TIME );
        return bitmap;
    }

    private Bitmap createCorrectBitmap( Bitmap srcBitmap, String correctResult ){

        Bitmap bitmap = srcBitmap.copy( Bitmap.Config.RGB_565, true );
        ArrayList<ImageCorrectBean> list = new Gson().fromJson( correctResult, new TypeToken<ArrayList<ImageCorrectBean>>() {}.getType());
        if( list==null || list.size()==0 ) return srcBitmap;

        Canvas canvas = new Canvas( bitmap );
        for( ImageCorrectBean bean : list ){
            drawCorrectMark( canvas, bean );
        }
        return bitmap;
    }

    private void drawCorrectMark( Canvas canvas, ImageCorrectBean correctBean ){

        int width = canvas.getWidth();
        int height= canvas.getHeight();

        Bitmap markBitmap;
        if( correctBean.getStatus() == ImageCorrectBean.CORRECT_RIGHT || correctBean.getStatus() == ImageCorrectBean.CORRECT_SMALL_RIGHT ){
            markBitmap = rightBitmap;
        }else if( correctBean.getStatus() == ImageCorrectBean.CORRECT_HALF || correctBean.getStatus() == ImageCorrectBean.CORRECT_SMALL_HALF ){
            markBitmap = halfBitmap;
        }else /*if( correctBean.getStatus() == ImageCorrectBean.CORRECT_WRONG  ) || correctBean.getStatus() == ImageCorrectBean.CORRECT_SMALL_WRONG */{
            markBitmap = wrongBitmap;
        }

        Rect srcRect = new Rect();
        srcRect.top = 0;
        srcRect.left = 0;
        srcRect.right = markBitmap.getWidth();
        srcRect.bottom= markBitmap.getHeight();

        Rect dstRect = new Rect();
        dstRect.left = (int)( width*correctBean.getPos().getX() );
        dstRect.top  = (int)(height*correctBean.getPos().getY() );
        dstRect.right = (int)(width*correctBean.getSize().getWidth() ) + dstRect.left;
        dstRect.bottom= (int)(height*correctBean.getSize().getHeight())+ dstRect.top;

        canvas.drawBitmap( markBitmap, srcRect, dstRect, new Paint());
    }
}
